package com.scyshkj.university.wechat.pay;

import com.scyshkj.university.wechat.utils.BindingResultUtils;
import com.scyshkj.university.wechat.utils.ErrorJsonDto;
import com.scyshkj.university.wechat.utils.JsonDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.IOException;

/**
 * Created by 唐旭 on 2016/11/21.
 */
@Controller
@RequestMapping("/pay")
public class WechatPayController {

    @Autowired
    private WechatPayService payService;

    @RequestMapping
    public String show() {
        return "redirect:/pay.html";
    }

    @RequestMapping(value = "/submit", method = RequestMethod.POST)
    @ResponseBody
    public JsonDto pay(@Valid RechargeFormDto formDto, BindingResult result) {
        if (result.hasErrors()) {
            return BindingResultUtils.buildErrorMessage(result, new ErrorJsonDto());
        }
        return payService.pay(formDto);
    }

    @RequestMapping(value = "/notify")
    @ResponseBody
    public String notifyRecharge(HttpServletRequest request) throws IOException {
        return payService.rechargeNotify(request);
    }

}
